package immutablecollections.misc;

import java.util.*;

public class TextUtils
{
     public static String repeat(String stringToRepeat, int repeatCount)
     {
          StringBuilder sb = new StringBuilder();
          for (int j = 0; j < repeatCount; j++)
          {
               sb.append(stringToRepeat);
          }
          return sb.toString();
     }

     public static String pad(String stringToPad, int width)
     {
          int gap = width - stringToPad.length();

          return gap == 0
                    ? stringToPad
                    : gap > 0
                              ? stringToPad + repeat(" ", gap)
                              : stringToPad.substring(0, width - gap - 1);
     }

     public static String join(Collection<?> thingsToJoin, String separator)
     {
          return join(thingsToJoin.iterator(), separator);
     }

     public static String join(Iterator<?> iterator, String separator)
     {
          StringBuilder sb = new StringBuilder();

          while (iterator.hasNext())
          {
               sb.append(iterator.next());
               if (iterator.hasNext())
                    sb.append(separator);
          }

          return sb.toString();
     }

     public static String join(Object[] items, String separator)
     {
          return join(Arrays.asList(items), separator);
     }
}
